package config

import (
    "fmt"
    "github.com/robfig/config"
    "os"
    "strings"
)

type IniFile struct {
    Null         bool
    Framework    string
    User         string
    RootPassword string
    Host         []string
    Port         int
    Dir          string
    Exec         []string
    Mode         string
    LocalBackDir string
    LocalBackUp  bool
    _modes       []string
}

func (file IniFile) IsIrrational() bool {
    if file.Mode == "" {
        fmt.Println("请指定Section(-s [section])")
        return true
    } else if len(file.Host) == 0 {
        fmt.Println("请配置或指定部署服务器Host")
        return true
    } else if file.User == "" {
        fmt.Println("请配置或指定SSH用户名User")
        return true
    } else if file.Dir == "" {
        fmt.Println("请配置或指定服务器部署目录(Dir)")
        return true
    }

    return false
}

func NewIniFile(path string, mode string, host string) (IniFile, error) {
    iniFile := IniFile{}
    ini, _ := config.ReadDefault(path)
    iniFile._modes = ini.Sections()
    //先读取默认配置项
    //框架
    iniFile.Framework, _ = ini.String("DEFAULT", "framework")
    //部署文件夹
    iniFile.Dir, _ = ini.String("DEFAULT", "dir")
    //默认用户名
    iniFile.User, _ = ini.String("DEFAULT", "user")
    //本地备份文件机制
    _localBackUp, err := ini.String("DEFAULT", "localBackUp")
    if err != nil && "false" == strings.ToLower(_localBackUp) {
        iniFile.LocalBackUp = false
    } else {
        iniFile.LocalBackUp = true
    }
    iniFile.LocalBackDir, _ = ini.String("DEFAULT", "localBackDir")
    _exec, _ := ini.String("DEFAULT", "exec")
    if _exec != "" {
        iniFile.Exec = strings.Split(_exec, ",")
    }
    if mode != "" {
        if !ini.HasSection(mode) {
            return iniFile, fmt.Errorf("没有找到配置项`%s`,你可以尝试 `-s [%s]`", mode, strings.Join(ini.Sections(), "/"))
        }
        //再读取附加配置项
        iniFile.Mode = mode
        //目标机器
        _host, _ := ini.String(mode, "host")
        iniFile.Host = strings.Split(_host, ",")
        //用户名
        _user, _ := ini.String(mode, "user")
        if _user != "" {
            iniFile.User = _user
        }
        //部署文件夹
        _dir, _ := ini.String(mode, "dir")
        if _dir != "" {
            iniFile.Dir = _dir
        }
        //本地备份文件机制
        _localBackUp, err := ini.String(mode, "localBackUp")
        if err != nil && "false" == strings.ToLower(_localBackUp) {
            iniFile.LocalBackUp = false
        } else {
            iniFile.LocalBackUp = true
            _localBackDir, err := ini.String(mode, "localBackDir")
            if err == nil {
                iniFile.LocalBackDir = _localBackDir
            }
        }
        //额外执行命令
        _exec, _ = ini.String(mode, "exec")
        if _exec != "" {
            _execs := strings.Split(_exec, ",")
            if len(iniFile.Exec) > 0 {
                iniFile.Exec = append(iniFile.Exec, _execs...)
            } else {
                iniFile.Exec = _execs
            }
        }
        iniFile.Null = false
    } else if host != "" {
        iniFile.Host = strings.Split(host, ",")
    } else {
        os.Exit(1)
    }
    if iniFile.LocalBackUp && "" == iniFile.LocalBackDir {
        fmt.Println("未指定本地备份文件夹,备份功能已停用!")
        iniFile.LocalBackUp = false
    }
    return iniFile, nil
}
